package com.oocl.scan.producer;

import com.oocl.scan.io.FileReader;
import com.oocl.scan.jdt.parser.ConstantParser;
import com.oocl.scan.model.Constant;
import com.oocl.scan.store.ConstantStore;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class ConstantProducer implements Producer<File> {

  ConstantParser parser = new ConstantParser();

  @Override
  public void produce(File file) {
    if (!file.exists()) {
      return;
    }
    if (FileReader.isNonTestJavaClass(file.getName())) {
      buildConstant(file.getAbsolutePath());
    }
  }

  private void buildConstant(String filePath) {
    try {
      String resource = FileReader.read(filePath);
      String moduleName = FileReader.getModuleName(filePath);
      List<Constant> constantList = parser.buildConstant(resource);
      for(int i = 0 ;i<constantList.size();i++){
        Constant constant = constantList.get(i);
        constant.setModule(moduleName);
        ConstantStore.getInstance().add(constant);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
